<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
<<<<<<< HEAD
use think\Session;
use app\admin\controller\Admin;
use PHPExcel_IOFactory;
use PHPExcel;
=======
use app\admin\controller\Admin;
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b


class Bids extends Admin
{
<<<<<<< HEAD

=======
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
    /**
     * 投标信息总览
     */
    public function index()
    {   
        //查询投标信息
<<<<<<< HEAD
        $list = db('bidding_project')->select();
        return view('admin@bids/index', [
            'list' => $list
        ]);
    }

    /**
     * 渲染投标申请页面
     * @return [type] [description]
     */
    public function projectAdd()
    {
        return view("admin@bids/projectAdd");
    }

    public function projectDoAdd()
    {   
         $file = request()->file([]);
         // var_dump($file);die;
         $data = input('param.');
        $data['status'] = 2;
        $data['demand_analysis'] = $file['demand_analysis']['name'];
        $data['project_proposal'] = $file['project_proposal']['name'];
        // var_dump($data);die;
        db('bidding_project')->insert($data);
        $details_id = Db::name('bidding_project')->getLastInsID();
        if ($details_id <= 0) {
            return $this->error('数据保存失败!请重新填写!');
        }
        // var_dump($details_id);die;
        // var_dump($data);die;
        $res = flow('经营部部长','项目立项申请','admin/bids/flowDetails',$details_id,2);
        if ($res > 0) {
            
            return $this->success('流程发起成功!', url('admin/bids/index'));
        } else {
            return $this->error('流程发起失败!');
        }
    }

    /**
=======
         $list = db('bidding_project')->select();
        // var_dump($list);die;

        // 渲染列表信息
         return view('admin@bids/index', [
             'list' => $list
         ]);
    }
     /**
     * 渲染投标立项申请模板
      */
      public function projectAdd()
     {
        return view('admin@bids/projectAdd');
     }

     public function projectDoAdd(){
        
    }

     /**
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
     * 查询数据并渲染详情页
     * @param  [type] $id 
     * @return [type]
      */
     public function details($id){

<<<<<<< HEAD
        // 接收ID 
        $id =  input('param.');
        //数据库查询信息 
        $data = db('bidding_project')->select($id);
        //渲染页面
        return view('admin@bids/details',[
            'data'=>$data
         ]);
     }
 
     public function flowDetails($id)
     {
        // 接收ID
        $id =  input('param.');
        //数据库查询信息 
        $data = db('bidding_project')->select($id);
        // var_dump($data);die;
        //渲染页面
        return view('admin@bids/flowDetails',[
=======
        // 接收ID
        $id =  input('param.');
        //数据库查询信息
        $data = db('bidding_project')->select($id);
        // var_dump($data);die;
        //渲染页面
        return view('admin@bids/details',[
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
            'data'=>$data
         ]);
     }

<<<<<<< HEAD
     public function doFlow($id)
     {

        $res = $this->agreeFlow($id,'经营部部长','总经理');
        var_dump($res); die;
        if ($res > 0) {
            return $this->success('已同意!', url('admin/Flow/index'));
        } else {
            return $this->error('同意失败!');
        }
     }
    
     public function dontFlow($id)
     {
        $res = $this->disagreeFlow($id,'经营部部长','总经理');
        if ($res > 0) {
            return $this->success('已同意!', url('admin/Flow/index'));
        } else {
            return $this->error('同意失败!');
        }
     }


     public function agreeFlow($id,$name1,$name,$status='')
     {
        $role_id_new = db('role')->field('id')->where(['name'=>$name])->find();
        $role_id = db('role')->field('id')->where(['name'=>$name1])->find();
        // var_dump($role_id,$role_id_new,$id);die;
        if ($status == '') {
             $res = db('role_flow')->where(['role_id'=>$role_id['id'],'details_id'=>$id])->update(['role_id' => $role_id_new['id']]);
            return $res;
        }else{
             $res = db('role_flow')->where(['role_id'=>$role_id['id'],'details_id'=>$id])->update(['flow_status' => $status]);
             return $res;
        }
     }

     public function disagreeFlow($id,$name1,$name,$status='')
     {
        $role_id_new = db('role')->field('id')->where(['name'=>$name])->find();
        $role_id = db('role')->field('id')->where(['name'=>$name1])->find();
        // var_dump($role_id,$role_id_new,$id);die;
        if ($status == '') {
             $res = db('role_flow')->where(['role_id'=>$role_id['id'],'details_id'=>$id])->update(['role_id' => $role_id_new['id']]);
            return $res;
        }else{
             $res = db('role_flow')->where(['role_id'=>$role_id['id'],'details_id'=>$id])->update(['flow_status' => $status]);
             return $res;
        }
     }


     /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        // echo 'read'; die;
        // 是否为Ajax请求
        // if (!Request::instance()->isAjax()) {
        //     $this->error('请求错误', '/');
        // }

        // 查询指定信息 显示查询
        $res = db('bidding_project')->where(['id'=>$id])->find();
        // var_dump($res);die;
        if ($res === null) {
            return false;
        } else {
            $res['success'] = ($res['success'] == 1 ?'是':'否');
            switch ($res['status']) {
                case '1': $res['status'] = '流程未发起'; break;
                case '9': $res['status'] = '流程已完结'; break;
                default:  $res['status'] = '流程审核中'; break;
            }
        }
        return json($res);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        $result = db('bidding_project')->delete($id);
        $res = db('role_flow')->where('details_id',$id)->delete();
        if ($res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的项目撤销成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '撤销失败';
        }
        return json($info);
    }


    public function fee()
    {
        $res = db('bidding_fee')->select();
        return view('admin@bids/fee',[
            'res'=>$res
        ]);
    }

    public function feeAdd()
    {
        $res =db('bidding_project')->field('bid_project')->select();
        return view('admin@/bids/feeAdd',[
            'res'=>$res
        ]);
    }

    public function feeDoAdd()
    {
        $data = input('param.');
        // var_dump($data);die;
        foreach ($data as $k => $v) {
            if($k == 'project_name'){
                continue;
            }
            @$data['total_cost'] += $v ;           
        }
        // var_dump($data);die;
        $res = db('bidding_fee')->insert($data);
        if ($res > 0) {
            return $this->success('添加成功!', url('admin/bids/fee'));
        } else {
            return $this->error('添加失败!');
        }
    }

    public function feeEdit()
    {
        $id = input('id');
        $res =db('bidding_project')->field('bid_project')->select();
        $data = db('bidding_fee')->where(['id'=>$id])->find();
        // var_dump($data,$res);die;
        return view('admin@/bids/feeEdit',[
            'res'=>$res,
            'data'=>$data
        ]);
    }

    public function feeDoEdit()
    {
        $data = input('param.');
        // var_dump($data);die;
        foreach ($data as $k => $v) {
            if($k == 'project_name'){
                continue;
            }
            @$data['total_cost'] += $v ;           
        }
        // var_dump($data);die;
        $res = db('bidding_fee')->where(['id'=>$data['id']])->update($data);
        if ($res > 0) {
            return $this->success('修改成功!', url('admin/bids/fee'));
        } else {
            return $this->error('修改失败!');
        }
    }

    public function delFee($id)
    {   
        $res = db('bidding_fee')->delete($id);
        if ($res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的项目撤销成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '撤销失败';
        }
        return json($info);
    }


    public  function fileUpload()
    {
      $file = request()->file();
      // 移动到框架应用根目录/public/uploads/ 目录下
      if($file){
          $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
          if($info){
              return true;
          }else{
              // 上传失败获取错误信息
              return false;
          }
    }
    }


    public function outExcel()
    {

        $data = db('bidding_fee')->select();
        // var_dump($data);die;
        $arr = [];
        $arr['id'] = '合计(元)';
        $arr['project_name'] = '';
        foreach ($data as $k => $v) {
          $n = $k;
          @$arr['buy_file_cost'] += $v['buy_file_cost'];
          @$arr['make_file_cost'] += $v['make_file_cost'];
          @$arr['service_cost'] += $v['service_cost'];
          @$arr['bond'] += $v['bond'];
          @$arr['else_cost'] += $v['else_cost'];
          @$arr['total_cost'] += $v['total_cost'];
        }
        
        $data[$n +1] = $arr;
        // var_dump($data);die;
        Vendor('phpexcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('phpexcel.PHPExcel.Worksheet.Drawing');
        Vendor('phpexcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A1");
        $letter =explode(',',"A1,B1,C1,D1,E1,F1,G1,H1");
        $arrHeader =  array('ID','项目名称','购买费用(元)','制作费用(元)','综合服务费(元)','保证金(元)','其它费用(元)','总计(元)');;
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($data as $k=>$v){
            // $k +=2;
            $objActSheet->setCellValue('A1'.$k,$v['id']);
            $objActSheet->setCellValue('B1'.$k, $v['project_name']);
            // // 图片生成
            // $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            // $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
            // // 设置宽度高度
            // $objDrawing[$k]->setHeight(40);//照片高度
            // $objDrawing[$k]->setWidth(40); //照片宽度
            // /*设置图片要插入的单元格*/
            // $objDrawing[$k]->setCoordinates('C'.$k);
            // // 图片偏移距离
            // $objDrawing[$k]->setOffsetX(30);
            // $objDrawing[$k]->setOffsetY(12);
            // $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            // 表格内容
            $objActSheet->setCellValue('C1'.$k, $v['buy_file_cost']);
            $objActSheet->setCellValue('D1'.$k, $v['make_file_cost']);
            $objActSheet->setCellValue('E1'.$k, $v['service_cost']);
            $objActSheet->setCellValue('F1'.$k, $v['bond']);
            $objActSheet->setCellValue('G1'.$k, $v['else_cost']);
            $objActSheet->setCellValue('H1'.$k, $v['total_cost']);
            // 表格高度
            // $objActSheet->getRowDimension($k)->setRowHeight(40);
        }

        $width = array(20,20,15,10,10,30,10,15);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[7]);
        $objActSheet->getColumnDimension('B')->setWidth($width[7]);
        $objActSheet->getColumnDimension('C')->setWidth($width[7]);
        $objActSheet->getColumnDimension('D')->setWidth($width[7]);
        $objActSheet->getColumnDimension('E')->setWidth($width[7]);
        $objActSheet->getColumnDimension('F')->setWidth($width[7]);
        $objActSheet->getColumnDimension('G')->setWidth($width[7]);
        $objActSheet->getColumnDimension('H')->setWidth($width[7]);


        $outfile = "投标项目费用统计表".date("Y-m-d").".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
        exit;
    }

    public function  addExccel(Request $request)
    {
        $file = $request->file('excel');

      if(empty($file)){
          $this->error('请上传你要导入的文件','/admin/Mechanical/forExccel');
      }

        if(request() -> isPost()){

            vendor("PHPExcel.PHPExcel");

        }
        //方法一

        $objPHPExcel =new \PHPExcel();
        //获取表单上传文件
       $file = request()->file('excel');
//        var_dump($file);exit;

       $info = $file->validate(['ext' => 'xlsx,xls'])->move(ROOT_PATH . 'public');

//        var_dump($info);exit;
       //上传验证后缀名,以及上传之后移动的地址  E:\wamp\www\bick\public
          if($info)
          {
//           echo $info->getFilename();

          $exclePath = $info->getSaveName();
          //获取文件名
          $file_name = ROOT_PATH . 'public' . DS . $exclePath;
            var_dump($file_name);
          //上传文件的地址
              $objReader =\PHPExcel_IOFactory::createReader( 'Excel5');
              $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');
          //加载文件内容,编码utf-8
           $excel_array=$obj_PHPExcel->getSheet(0)->toArray();

           //转换为数组格式
           array_shift($excel_array);

           //删除第一个数组(标题);
           $city = [];

           $i=0;
           foreach($excel_array as $k=>$v) {
//               $city[$k]['id'] = $v[0];
               $city[$k]['project_name'] = $v[1];
               $city[$k]['machine_name'] = $v[2];
               $city[$k]['freight'] = $v[3];
               $city[$k]['maintenance_cost'] = $v[4];
               $city[$k]['oil_cost'] = $v[5];
               $city[$k]['machine_usage_cost'] = $v[6];
               $city[$k]['total_price'] = $v[7];
               $i++;

           }
              unset($city['0']);
//           var_dump($city);exit;
              $res =db("mechanical_cost")->insertAll($city);
//                var_dump($res);
             }else{

              echo $file->getError();

           }

         return $this->fetch("admin@mechanical/forExccel");


    }
=======
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b


}